
<?php

class App_Controllers_article
{
    private $_template;
    private $_models = array();

    public function index()
    {
        $this->_models['article'] = new App_Models_Article();


        $this->_template = new Core_Template();

        $this->_template->add_css_file(array('/App/Views/index.css',                                             
                                             '/App/Views/article/article.css'                                             
                                       ));


    


        $this->_template->set_title("Новости");
        $this->_template->get_smarty()->assign('title',$this->_template->get_title());
        $this->_template->get_smarty()->assign('css_file',$this->_template->get_css_file());



        $this->_template->get_smarty()->assign('content','article/articles');
        $this->_template->get_smarty()->assign('article',$this->_models['article']->get_articles());
        $this->_template->get_smarty()->display('index.tpl');
    }

    public function read($request)
    {
        $this->_models['article'] = new App_Models_Article();
        $this->_models['comment'] = new App_Models_Comment();

        $this->_template = new Core_Template();

        $this->_template->add_css_file(array('/App/Views/index.css',                                                
                                                 '/App/Views/article/article.css',  
                                                 '/App/Views/comment/comment.css'
                                           ));

        $this->_template->add_js_file(array(
        		'/App/Views/js/ajax.js',
        		'/App/Views/js/jquery.js',
        		'/App/Views/js/jquery.corner.js',
        		'/App/Views/js/comment.js'
        ));

        if (isset ($_GET['id'])) {
            if ( 0 > $_GET['id']) {
                $this->_template->error_page();
            } else {
                $article = $this->_models['article']->get_article($_GET['id']);
                if (false === $article) {
                    $this->_template->error_page();
                }
                $article = $article[0];
            }
        }

  

        $comments = $this->_models['comment']->get_comments("articlecomment","article_id",$_GET['id']);

        $this->_template->get_smarty()->assign('comments',$comments);


        $this->_template->set_title($article['title']);

        $this->_template->get_smarty()->assign('title',$this->_template->get_title());
        $this->_template->get_smarty()->assign('css_file',$this->_template->get_css_file());
        $this->_template->get_smarty()->assign('js_file',$this->_template->get_js_file());


        $this->_template->get_smarty()->assign('content','article/article');
        $this->_template->get_smarty()->assign('article',$article);
        $this->_template->get_smarty()->display('index.tpl');
    }

    public function add_comment()
    {
        
        if ((isset($_POST['author'])) && (isset($_POST['text'])) && (isset($_POST['email']))) {
            if ((!empty($_POST['author'])) && (!empty($_POST['text'])) && (!empty($_POST['email']))) {
                $this->_models['comment'] = new App_Models_Comment();
                $this->_models['comment']->add_comment("articlecomment","article_id",$_GET['id'], $_POST['author'], $_POST['email'], $_POST['text']);
      
                echo  $_POST['author']."|#|". date("Y-m-d H:i:s")."|#|". $_POST['text'];
            } else {
                echo "1|#|none|#|2";
            }
        }
        
    }

    public function __call($name, $args)
    {
        $this->_template = new Core_Template();
        $this->_template->error_page();
    }
}

?>
